package as3.component.controls
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import as3.component.geom.IntRectangle;
	import as3.component.manager.PopupManager;
	import as3.component.support.BaseButton;
	import as3.component.support.DataProvider;
	

	public class ComboBox extends BaseButton
	{
//		[Embed(source="assets/ui/Border7Skin.png", scaleGridLeft="8", scaleGridRight="30", scaleGridTop="8", scaleGridBottom="30")]
		public var Black_Bg:Class;
		protected var lineBg:DisplayObject=new Black_Bg() as DisplayObject;
		
		protected var _leftPadding:Number = 8;
		protected var _rightPadding:Number = 20;
		protected var _dropdownWidth:Number;
		
		public var _textField:Label;
		
		protected var _fontBold:Boolean;
		protected var _fontSize:uint;
		protected var _fontColor:uint = 0x999999;
		
		private var _labelField:String;
		protected var _dropdown:List;
		
		private var minWidth:Number = 35;
		private var _rowCount:int = 5;
		
		private var dropShow:Boolean = false;
		
		private var _selectItem:Object;
		private var _selectIndex:Object;
		
		private var listBg:Sprite;
		private var _upImage:String;
		private var _overImage:String;
		private var _downImage:String;
		private var _disabledImage:String;
		public function ComboBox(upImage:String="ComboBox_upSkin",overImage:String="ComboBox_overSkin",downImage:String="ComboBox_downSkin",disabledImage:String="ComboBox_disabledSkin")
		{
			_upImage=upImage;
			_overImage=overImage;
			_downImage=downImage;
			_disabledImage=disabledImage;
			super();
		}
		
		//设置UI信息
		override protected function initUI():void
		{
			super.initUI();
			_uiLibrary["upImage"] = _upImage;
			_uiLibrary["overImage"] = _overImage;
			_uiLibrary["downImage"] = _downImage;
			_uiLibrary["disabledImage"] = _disabledImage;
			_fontColor = _defaultSkin.comboBoxTabel.fontColor || _fontColor;
			_fontBold = _defaultSkin.btnCssTable.fontBold || false;
			_fontSize = _defaultSkin.btnCssTable.fontSize || 12;
		}
		
		override protected function initCom():void
		{
			super.initCom();
			setMasked(true);
			_textField = new Label();
			_textField.color = _fontColor;
			_textField.selectable = false;
			_textField.bold = _fontBold;
			_textField.fontSize = _fontSize;
			_textField.mouseChildren = _textField.mouseEnabled = false;
			addChild(_textField);
			_textField.width = 0;
			_textField.height = 20;
			_textField.x = _leftPadding;
			creatList();
			width = minWidth;
		}
		
		protected function creatList():void
		{
			_dropdown = new List();
			_dropdown.width = minWidth;
			_dropdown.height = 22;
			_dropdown.verticalScrollPolicy = ScrollBarPolicy.AUTO;
			//_dropdown.setCellRendererClass(FuncMapCellRenderer);
		}
		
		override protected function initListener():void
		{
			super.initListener();
			addEventListener(MouseEvent.CLICK, $onComboBoxClick);
			_dropdown.addEventListener(MouseEvent.MOUSE_DOWN, $onDropDownClick);
			_dropdown.addEventListener(Event.SELECT, $onListSelect);
		}
		
		private function $onComboBoxClick(evt:MouseEvent):void
		{
			evt.stopImmediatePropagation();
			if (!dropShow) {
				onPopUpOpen();
			} else {
				onPopUpClose();
			}
		}
		
		private function $onDropDownClick(evt:MouseEvent):void
		{
			evt.stopImmediatePropagation();
		}
		
		private function $onListSelect(evt:Event):void
		{
			if (_selectItem == selectItem && _selectIndex == selectIndex) return;
			_selectItem = selectItem;
			_selectIndex = selectIndex;
			labelField = String(_selectItem.label);
			dispatchEvent(new Event(Event.CHANGE));
			onPopUpClose();
		}
		
		public function get selectItem():Object
		{
			return dropdown.selectItem;
		}
		
		public function get selectIndex():int
		{
			return dropdown.selectIndex;
		}
		
		public function set selectIndex(value:int):void
		{
			dropdown.selectIndex = value;
		}
		
		public function set rowCount(value:int):void
		{
			_rowCount = value;
			if (dropdown.length > rowCount) {
				dropdown.height = dropdown.getCellHeight() * rowCount;
			} else {
				dropdown.height = dropdown.getCellHeight() * dropdown.length;
			}
		}
		
		public function get rowCount():int
		{
			return _rowCount;
		}
		
		public function set labelField(value:String):void
		{
			if (value == labelField) return;
			_labelField = value;
			_textField.text = _labelField;
			updateWidth();
		}
		
		public function get labelField():String
		{
			return _labelField;
		}
		
		public function get dropdown():List
		{
			return _dropdown;
		}
		
		public function set dataProvider(value:DataProvider):void
		{
			_selectItem = null;
			_selectIndex = -1;
			dropdown.dataProvide = value;
			rowCount = rowCount;
		}
		
		public function get dataProvider():DataProvider
		{
			return _dropdown.dataProvide;
		}
		
		override public function set width(value:Number):void
		{
			super.width = value;
			dropdown.width = value;
			updateRectangle();
		}
		
		override public function get height():Number
		{
			return _simpleButton.height;
		}
		
		protected function updateRectangle():void
		{
			setMaskRect(new IntRectangle(0,0,simpleButton.width, simpleButton.height));
		}
		
		protected function updateWidth():void
		{
			_textField.width = _textField.textField.textWidth + 8;
			_textField.height = _textField.textField.textHeight + 4;
			_textField.y = (height - _textField.height) * 0.5;
//			var minW:Number = _textField.width + _leftPadding + _rightPadding;
//			if (_dropdownWidth > minW) {
//				width = minW;
//			} else {
//				width = minW;
//				_dropdownWidth = minW;
//				dropdown.width = _dropdownWidth;
//			}
		}
		
		override public function onPopUpOpen():void
		{
			setListBg()
			super.onPopUpOpen();
			var p:Point = this.localToGlobal(new Point(0, height));
			p= new Point(p.x,p.y-2);
			PopupManager.getInstance().addPopUp(listBg, p);
			dropShow = true;
			PopupManager.getInstance().registClick($onOtherClick);
		}
		
		override public function onPopUpClose():void
		{
			super.onPopUpClose();
			if(listBg)
				PopupManager.getInstance().removePopUp(listBg);
			dropShow = false;
			PopupManager.getInstance().unRegist($onOtherClick);
		}
		
		private function $onOtherClick(evt:MouseEvent):void
		{
			onPopUpClose();
		}
		
		private function setListBg():void
		{
			lineBg.width=dropdown.width;
			lineBg.height=dropdown.height;
			listBg= new Sprite();
			listBg.addChild(lineBg);
			listBg.addChild(dropdown);
			dropdown.move(8,0);
			dropdown.setSize(107,92);
		}
	}
}